package multiThreading;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class D {
    public static void main(String arg[]) {
        System.out.println("电脑核数为：" + Runtime.getRuntime().availableProcessors());//窃取线程，即当有些线程完成自己的任务后，回去窃取其他线程未完成的任务
        ExecutorService pool = Executors.newWorkStealingPool();
        pool.execute(new d1(2000));//该线程模拟一个长时间未完成的线程
        for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
            pool.execute(new d1(2000));//模拟一个比较快执行完的线程
        }
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class d1 implements Runnable {
    private int time;

    public d1(int time) {
        this.time = time;
    }

    @Override
    public void run() {
        try {
            TimeUnit.MILLISECONDS.sleep(time);//用time作为每次线程运行的时间间隙，用于找出被窃取的线程
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ":" + time);
    }
}
